package com.teamin.service.impl.wx;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.teamin.security.AESUtils;
import com.teamin.web.Evn;
import com.teamin.web.https.OkHttpRequest;
import okhttp3.*;

import java.io.IOException;

public class SmallMessageManager {

	public static String SMALL_CARD="b7rzAXvN01CydgF3" ;
	public static final String TEMPLATE_URI = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=";

	public static void sendTemplateMessage(Object obj) {

		if("test".equals(Evn.SERVICE_PROFILS)) {
			OkHttpRequest.httpsRequest(Evn.SERVICE_URL_SMALLCARD,"POST",null,AESUtils.encrypt(JSONObject.toJSONString(obj),SMALL_CARD)) ;
		}else {
			OkHttpClient okHttpClient = new OkHttpClient();
			MediaType mediaType = MediaType.parse("application/json");
			//创建RequestBody对象，将参数按照指定的MediaType封装
			RequestBody requestBody = RequestBody.create(mediaType,JSONObject.toJSONString(obj));
			Request request = new Request.Builder().url(TEMPLATE_URI + WeChatSmallProgramTokenManager.getInstance().getAccessToken()).post(requestBody).build();
			Call call = okHttpClient.newCall(request);
			Response response = null;
			try {
				response = call.execute();
				JSONObject asscessTokenObject = JSON.parseObject(response.body().string());
				System.out.println(asscessTokenObject.toJSONString());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
